/*
 *  ClientList.c
 *  ClientDispatch
 *
 *  Created by Andrey on 14/10/2008.
 *  Copyright 2008 ИУ7-2003. All rights reserved.
 *
 */

#include <stdlib.h>
#include <string.h>

#include "ClientList.h"

TAILQ_HEAD(CDClientList, _CDClientInfo) _cdClientList = TAILQ_HEAD_INITIALIZER(_cdClientList);

void InitClientList()
{
	TAILQ_INIT(&_cdClientList);
}

void AddClientToList(CDClientInfoPtr clientInfo)
{
	TAILQ_INSERT_TAIL(&_cdClientList, clientInfo, clientListEntries);
}

CDClientInfoPtr GetClientInfoByAddress(AsciiStringPtr clientAddress)
{
	CDClientInfoPtr listEntry;
	
	TAILQ_FOREACH(listEntry, &_cdClientList, clientListEntries)
	{
		if (listEntry != NULL && strcmp(listEntry->clientAddress, clientAddress) == 0)
			return listEntry;
	}
	
	return NULL;
}

CDClientInfoPtr RemoveClientFromList(AsciiStringPtr clientAddress)
{
	TAILQ_REMOVE(&_cdClientList, GetClientInfoByAddress(clientAddress), clientListEntries);
	
	return NULL;
}

void PurgeClientList()
{
	CDClientInfoPtr queueEntry = (CDClientInfoPtr)TAILQ_FIRST(&_cdClientList);
	CDClientInfoPtr nextQueueEntry;
	
	while (queueEntry != NULL) 
	{
		nextQueueEntry = (CDClientInfoPtr)TAILQ_NEXT(queueEntry, clientQueueEntries);
		free(queueEntry);
		queueEntry = nextQueueEntry;
	}
}